package cn.buk.qms;

import cn.buk.common.dto.eterm.PnrFlightDto;
import cn.buk.common.dto.eterm.PnrPassengerDto;
import cn.buk.tms.entity.order.flight.FlightOrder;
import cn.buk.tms.entity.order.flight.FlightOrderFlight;
import cn.buk.tms.entity.order.flight.FlightOrderPassenger;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import cn.buk.tms.common.dto.base.BasePriceDto;
import cn.buk.tms.flight.request.FlightOrderRequestDto;
import cn.buk.tms.util.IdUtil;
import cn.buk.common.util.DateUtil;
import org.springframework.beans.BeanUtils;

import static cn.buk.common.Constant.*;
import static cn.buk.tms.common.constant.TmsConstant.ID_TYPE_NI;

/**
 * @author yfdai
 */
public class OrderUtil {

    private OrderUtil() {
        //hide constructor
    }

    public static Date getTktlDate(final Date tktlDate, final String tktlTime) {
        String strHour = tktlTime.substring(0, 2);
        String strMinute = tktlTime.substring(2, 4);

        int intHour = Integer.parseInt(strHour);
        int intMinute = Integer.parseInt(strMinute);
        if (intHour >= 0 && intHour <= 23 && intMinute >= 0 && intMinute < 60) {
            Calendar c = Calendar.getInstance();
            c.clear();
            c.setTime(tktlDate);
            c.add(Calendar.HOUR_OF_DAY, intHour);
            c.add(Calendar.MINUTE, intMinute);

            return c.getTime();
        } else {
            return tktlDate;
        }
    }

    public static void copyFlightOrderPrice(FlightOrderRequestDto dto, FlightOrder info) {
        dto.calcCost(); // 需要计算成人和儿童的数量

        if (dto.getAdultCount() > 0) {
            BasePriceDto o = dto.getAdultPrice();
            // 成人价格
            info.setPrice(o.getPrice());
            info.setParvalue(o.getParValue());
            info.setTax(o.getTax());
            info.setInsurance(o.getInsurance());
            info.setServiceCharge(o.getServiceCharge());
            info.setCommission(o.getCommission());
            info.setCommission2(o.getCommission2());
            info.setDiscount(o.getDiscount());
            info.setAmount(o.getAmount());
            info.setCost(o.getCost());
        }

        if (dto.getChildCount() > 0) {
            // 儿童价格
            BasePriceDto o = dto.getChildPrice();
            info.setChdPrice(o.getPrice());
            info.setChdParvalue(o.getParValue());
            info.setChdTax(o.getTax());
            info.setChdInsurance(o.getInsurance());
            info.setChdServiceCharge(o.getServiceCharge());
            info.setChdCommission(o.getCommission());
            info.setChdCommission2(o.getCommission2());
            info.setChdDiscount(o.getDiscount());
            info.setChdAmount(o.getAmount());
            info.setChdCost(o.getCost());
        }

        if (dto.getInfantCount() > 0) {
            // 婴儿价格
            BasePriceDto o = dto.getInfantPrice();
            info.setInfPrice(o.getPrice());
            info.setInfParvalue(o.getParValue());
            info.setInfTax(o.getTax());
            info.setInfInsurance(o.getInsurance());
            info.setInfServiceCharge(o.getServiceCharge());
            info.setInfCommission(o.getCommission());
            info.setInfCommission(o.getCommission2());
            info.setInfDiscount(o.getDiscount());
            info.setInfAmount(o.getAmount());
            info.setInfCost(o.getCost());
        }

        info.setTotalAmount(dto.getTotalAmount());
        info.setTotalCost(dto.getTotalCost());
        info.setProfit(dto.getTotalProfit());
    }

    public static void copyFlightOrderFlights(FlightOrderRequestDto dto, FlightOrder info) {
        List<FlightOrderFlight> flts = info.getFlights();

        int segOrd = 0;
        for (PnrFlightDto flt : dto.getFlights()) {
            segOrd++;

            FlightOrderFlight detail = new FlightOrderFlight();

            BeanUtils.copyProperties(flt.getFlight(), detail.getFlight());

            detail.getFlight().setCabinClass(flt.getFlight().getCabinClass());
            detail.getFlight().setDdate(flt.getFlight().getDdateFormatted());


            detail.setEnterpriseId(info.getEnterpriseId());
            detail.setSegOrder(segOrd);
            detail.setOrder(info);

            flts.add(detail);
        }

        int segCount = flts.size();
        info.setSegCount(segCount);
        if (segCount == 1) {
            info.setRouteType(1);
        } else if (segCount == 2) {
            if (flts.get(0).getFlight().getArrivalAirport().equals(flts.get(1).getFlight().getDepartureAirport())) {
                info.setRouteType(2);
            } else {
                info.setRouteType(3);
            }
        } else {
            info.setRouteType(3);
        }
    }

    public static void copyFlightOrderPassengers(FlightOrderRequestDto dto, FlightOrder info) {
        List<FlightOrderPassenger> psgs = info.getPassengers();

        for (PnrPassengerDto psg : dto.getPassengers()) {
            if (!psg.isSelected()) {
                continue;
            }

            FlightOrderPassenger detail = new FlightOrderPassenger();

            BeanUtils.copyProperties(psg, detail);

            detail.setEnterpriseId(info.getEnterpriseId());
            detail.setOrder(info);
            detail.setName(psg.getPsgName());
            detail.setIdNo(psg.getIdNo());
            detail.setPsgType(psg.getPsgType());

            if (psg.getIdType() == ID_TYPE_NI && psg.getIdNo().trim().length() == 18) {
                detail.setBirthday(IdUtil.retrieveBirthday(psg.getIdNo().trim()));
                detail.setGender(IdUtil.retrieveGender(psg.getIdNo().trim()));
                if (psg.getBirthday() != null) {
                    final int age = DateUtil.calcAge(psg.getBirthday());
                    if (age > 12) {
                        detail.setPsgType(PSG_TYPE_ADU);
                    } else if (age > 2) {
                        detail.setPsgType(PSG_TYPE_CHD);
                    } else {
                        detail.setPsgType(PSG_TYPE_INF);
                    }
                }
            }

            psgs.add(detail);
        }

        info.setPsgCount(psgs.size());
    }

    public static String cleanMrInName(final String psgName) {
        String regEx = "(MR|MS|MSTR)$";

        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(psgName);

        String newPsgName = null;
        if (m.find()) {
            newPsgName = psgName.substring(0, m.start()).trim();
        } else {
            newPsgName = psgName;
        }

        return newPsgName;
    }

}
